import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchValue: "",
    farmerList: []
  },

  // 搜索小工姓名、手机号码
  onSearch() {
    this.getData();
  },

  // 跳转详情
  toDetails(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/management/farmer-details/index?id=" + id
    });
  },

  onShow() {
    this.getData();
  },

  getData() {
    wx.showLoading({
      title: "获取中..."
    });
    wx.$apis
      .getMarketUser({
        keyword: this.data.searchValue,
        current: 1,
        size: 100,
        companyId: app.globalData.userInfo.companyData.companyId,
        marketRoleId: 2
      })
      .then((res) => {
        wx.stopPullDownRefresh();
        if (res.data) {
          this.setData({
            farmerList: res.data.records.map((item) => {
              return {
                ...item,
                createTime: wx.$dayjs(item.createTime).format("YYYY-MM-DD HH:mm:ss")
              };
            })
          });
        }
      });
  },

  onDelete(event) {
    const id = event.target.dataset.id;
    Dialog.confirm({
      message: "确定删除吗？"
    }).then(() => {
      this.delete(id);
    });
  },

  // 删除亦是解绑
  delete(id) {
    // debugger
    wx.showLoading({
      title: "删除中..."
    });
    wx.$apis
      .deleteMarketUser({
        marketUserIds: [id],
        marketRoleId: 2
      })
      .then((res) => {
        this.getData();
        if (!res.data) {
          return wx.showToast({
            title: "删除失败，请稍后再试~",
            icon: "none",
            duration: 2000
          });
        }
        wx.showToast({
          title: "删除成功",
          icon: "success",
          duration: 2000
        });
      });
  },

  toAddFarmer() {
    wx.navigateTo({
      url: "/pages/management/add-farmer/index"
    });
  },
  onPullDownRefresh() {
    this.getData();
  },
  onReachBottom() {
    // this.getData()
  }
});
